/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.submit;

import java.io.StringWriter;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

public class TemplateGenerator implements CommandGenerator
{

	public static final String TMPL_KEY="_CMD_TEMPLATE";
	public static final String NAME="TEMPLATE";
	public String getName()
	{
		return NAME;
	}
	public String generateScript(ProgramValueBean bean, UnfinishedJob job) throws Exception
	{
		String templateName=bean.getValue(TMPL_KEY);
		if (templateName==null)
		{
			throw new Exception("No template is configured for this program");
		}
		
		VelocityContext context = new VelocityContext(bean.getValueMap());
		StringWriter w = new StringWriter();
		Velocity.mergeTemplate(templateName, "UTF-8", context, w);
		return w.toString();
	}
	public boolean postProcessMPI()
	{
		return false;
	}
}
